<template>
	<el-dialog :title="!dataForm.id ? '新建工序信息' : '编辑工序信息'" :close-on-click-modal="false" :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="100px" label-position="right" :rules="rules">
					<el-col :span="24">
						<el-form-item label="工序名称" prop="fullName">
							<el-input v-model='dataForm.fullName' placeholder='请输入' clearable required :style='{"width":"100%"}' maxlength='500' show-word-limit>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="工序编码" prop="enCode">
							<el-input v-model='dataForm.enCode' placeholder='请输入' clearable required :style='{"width":"100%"}' maxlength='50' show-word-limit>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
                    	<el-form-item label="车间" prop="workshopCode">
							<SHZY-select :list="siteList" placeholder="请选择车间"
								v-model:bindValue="dataForm.workshopCode" isConcat label="fullName" value="enCode" @change="GetStation"></SHZY-select>
						</el-form-item>
                	</el-col>
					<el-col :span="24">
						<el-form-item label="工位" prop="stationCode">
							<SHZY-select :list="stationOptions" placeholder="请选择工位"
								v-model:bindValue="dataForm.stationCode" isConcat label="fullName" value="enCode"></SHZY-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="有效标志" prop="enabledMark">
							<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0' >
							</el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="说明描述" prop="desc">
							<el-input v-model='dataForm.desc' placeholder='请输入' show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import { GetInfoFy, CreateModeling, updateModeling, getWorkStationInfo } from '@/api/common/modeling'
	import siteApi from "@/api/common/factorystructure"
	export default {
		components: {  },
		props: [],
		data() {
            const validatePass = (rule, value, callback) => {
                if (!value || value.trim().length == 0) {
                    callback(new Error("不能为空"));
                } else if (value.trim().length > 50) {
                    callback(new Error("长度不能超过50字"));
                } else {
                    callback();
                }
            }
            const validatePassone = (rule, value, callback) => {
				if (!value || value.trim().length == 0) {
					callback(new Error("不能为空"));
				} else if (value.trim().length > 500) { 
					callback(new Error("长度不能超过500字"));
				} else {
					callback();
				}
			};
			return {
				btnLoading:false,
				loading: false,
				visible: false,
				dataForm: {
					id:'',
					fullName:undefined,
					enCode:undefined,
					stationCode: undefined,
					workshopCode: undefined,
					desc: undefined,
					enabledMark: 1,
                    
				},
				rules: {
					fullName:[
						{
							required:true,
							message: '请输入工序名称',
							validator: validatePassone,
							trigger:'"blur"'
						},
					],
					enCode:[
						{
							required:true,
							message: '请输入工序编码',
							validator: validatePass,
							trigger:'"blur"'
						},
					],
				},
				stationOptions:[],
				siteList:[],
			}
		},
		computed: {},
        watch: {},
		created() {},
		mounted() {},
		methods: {
			goBack() {
                this.$emit('refresh')
            },
			init(id) {
				this.dataForm.id = id || 0;
                this.visible = true;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					
					siteApi.getWorkshopList().then(res => {
						console.log(res.data.list);
						this.siteList = res.data.list;
					});

					if (this.dataForm.id) {
						GetInfoFy(this.dataForm.id).then(res => {
							console.log(res.data);
							this.dataForm = res.data;
							getWorkStationInfo(this.dataForm.workshopCode).then(res => {
                			this.stationOptions = res.data.list
            				})
							if (!this.dataForm.filePath) this.dataForm.filePath = [];
						})
					}
					
				});
			},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					const formMethod = !this.dataForm.id ? CreateModeling : updateModeling;
					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
		},
        GetStation(data,stationCode) {
			
			this.dataForm.stationCode = stationCode;
			this.stationOptions=[];
            getWorkStationInfo(data).then(res => {
				if(res.data.list.length == 0)
                {
                    this.dataForm['stationCode'] = undefined
                }
				this.dataForm['stationCode'] = undefined
                this.stationOptions = res.data.list
            })
        }
		}
	}
</script>
